﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SensorNetworkUI;
using CenterSpace.NMath.Stats;
using CenterSpace.NMath.Core;
using System.Data.SqlClient;

namespace WebAccess
{
    public partial class Outlier : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {
                if (Session["Username"] == null)
                    Response.Redirect("~/login.aspx");
                
                
            }
        }

        public bool Test(SensorStructure sens)
        {

            SqlConnection conn = new SqlConnection(Constants.ConnectionString);
            SqlCommand comm = conn.CreateCommand();
            double mean = 0;
            double sd = 0;
            double sammean = 0;
            comm.CommandText = "select STDEV(temp_reading) as standard,AVG(temp_reading) as mean from reading where sensor_id = " + sens.Id;
            try
            {
                conn.Open();
                SqlDataReader read = comm.ExecuteReader();
                if (read.Read())
                {
                    mean = read.GetDouble(0);
                    sd = read.GetDouble(1);
                }
            }
            finally
            {
                conn.Close();


            }
            int n = 0;
            DateTime sel = StartDate.SelectedDate;
            comm.CommandText = "select STDEV(temp_reading) as standard,AVG(temp_reading) as mean, count(*) as n from reading where  reading_date > '" + sel.ToString() + "' and sensor_id = " + sens.Id;
            try
            {
                conn.Open();

                SqlDataReader rd = comm.ExecuteReader();
                rd.Read();
                sammean = rd.GetDouble(0);
                sd = rd.GetDouble(1);
                n = rd.GetInt32(2);
            }
            finally
            {
                conn.Close();
            }


            OneSampleTTest test = new OneSampleTTest(sammean, sd, n, mean);
            test.Alpha = 0.01;
            return test.Reject;





        }
        protected HashSet<SensorStructure> available_sensors()
        {



            UserDataStructure refe = (UserDataStructure)Session["Username"];
            HashSet<SensorStructure> sensors = new HashSet<SensorStructure>();
            if (refe == null)
                Response.Redirect("~/login.aspx");
            foreach (NetworkDataStructure net in refe.AllowedNetworks)
            {

                foreach (SensorStructure st in net.Sensors)
                {
                    sensors.Add(st);

                }

            }

            return sensors;

        }

    
        protected void Calendar2_SelectionChanged1(object sender, EventArgs e)
        {

            // AccordionPane1.Visible = true;
            ListBox1.Items.Clear();
            ListBox2.Items.Clear();
            foreach (SensorStructure sens in available_sensors())
            {

                if (Test(sens))
                {

                    ListBox2.Items.Add(new ListItem(sens.Name, sens.Id.ToString()));

                }
                else
                {
                    ListBox1.Items.Add(new ListItem(sens.Name, sens.Id.ToString()));
                }


            }
        }
    }
}